import learning as l
import time
from Categories import Categories
from Words import Words
from Files import Files

MAX_MOT = 10
MIN_CAT = 0
STEP_CAT = 25
MAX_CAT = 300


def countNoClues(noCluesInt, allWords, noCluesDone, time, testFiles):
    print ' '
    if noCluesDone == False:
        print "[", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "] Verifie Nb Fichiers 'NO-CLUE' START"
        print "*ATTENTION: ceci peu prendre plusieurs heures"
        print "*Je prends environ 4 minutes me rendre ici et la prochaine etape m'a pris >6h avec 1 a 9"

        for i in range(1,MAX_MOT,1):
            print "[", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "] Calcul avec minimum de",i
            keysS = allWords[i].bag.keys()
            keysS.sort()
            noCluesInts[i] = len(filter( None, map( lambda y: sum( map( lambda x: keysS.__contains__(x),y[1][1].keys())) == 0, testFiles.bag.items())))

        noCluesDone = True
        print "[", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "] Verifie Nb Fichiers 'NO-CLUE' STOP"

        print ' '

    for i in range(1,MAX_MOT,1):
        print 'Nb Fichiers "NO-CLUE" avec minimum de',i,':',noCluesInts[i]

    print ' '

def countCategories(testCatsS,learnCats,testFiles,learnFiles,c):
    print ' '
    print '== CATS',c,'=='

    learCatsS = map(lambda y: y[0],filter(lambda x: x[1][1] > c,learnCats))
    learCatsS.sort()
    catsOnlyLear = filter( lambda x: testCatsS.__contains__(x) == False, learCatsS)
    catsBoth = filter( lambda x: testCatsS.__contains__(x), learCatsS)
    catsOnlyTest = filter( lambda x: learCatsS.__contains__(x) == False, testCatsS)
    catsAll = catsOnlyLear + catsOnlyTest + catsBoth
    catsAll.sort()

    print "Categories in learn:",len(learCatsS)
    print "Categories in test:",len(testCatsS)
    print "Categories present in both:",len(catsBoth),"/",len(catsAll),"(",(len(catsBoth)*100/len(catsAll)),"% )"
    print "Categories present in Learn Only:",len(catsOnlyLear),"/",len(catsAll),"(",(len(catsOnlyLear)*100/len(catsAll)),"% )"
    print "Categories present in Test Only:",len(catsOnlyTest),"/",len(catsAll),"(",(len(catsOnlyTest)*100/len(catsAll)),"% )"

    usableLearFiles = filter( lambda x: catsBoth.__contains__(x[1][2]), learnFiles )
    usableTestFiles = filter( lambda x: catsBoth.__contains__(x[1][2]), testFiles )
    print "Files in learn on usable categories:",len(usableLearFiles),"/",len(learnFiles),"(",(len(usableLearFiles)*100/len(learnFiles)),"% )"
    print "Files in test on usable categories:",len(usableTestFiles),"/",len(testFiles),"(",(len(usableTestFiles)*100/len(testFiles)),"% )"


#-----------------------------------------------------------------------------#

print ' '

print '::: STATS :::'
print 'Stats mots minimums de 1 a',MAX_MOT-1

print ' '

print "[", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "] Read Test Files START"
testWords = Words()
testCats = Categories()
testFiles = Files()
folders = ('awards_1993','awards_1994')
l.readFiles(l.PATH, folders, testWords, testCats, l.stopwords, testFiles)
print "[", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "] Read Test Files STOP"

print ' '

allWords = {}
allWords[1] = Words()
allWords[2] = l.allwords
for i in range(3,MAX_MOT,1):
    allWords[i] = Words()

print "[", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "] Make new Bags START"
print 'Bagging ...'
for f in l.files.bag.items():
    allWords[1].addWords(f[1][1])
    for i in range(3,MAX_MOT,1):
        allWords[i].addWords(f[1][1])
print 'Pruning ...'
allWords[1].prune(1,l.MAX_FREQ_WORD)
for i in range(3,MAX_MOT,1):
    allWords[i].prune(i,l.MAX_FREQ_WORD)
    
print "[", time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), "] Make new Bags STOP"

print ' '

for i in range(1,MAX_MOT,1):
    print 'Nb Mots conserves avec minimum de',i,':',len(allWords[i].bag)

print ' '

noCluesInts = {}
noCluesDone = False
#countNoClues(noCluesInts, allWords, noCluesDone, time, testFiles)

testCatsS = testCats.bag.keys()
testCatsS.sort()
for c in range(MIN_CAT,MAX_CAT+1,STEP_CAT):
    countCategories(testCatsS,l.categories.bag.items(),testFiles.bag.items(),l.files.bag.items(),c)
